#include "EventWindow.h"

#include "EventItem.h"

#include <object_v2/object_v2.h>

struct _EventWindow
{
        AdwApplicationWindow parent_instance;

        /*模板部件*/
        GtkButton         *add_button;
        AdwHeaderBar      *header_bar;
        GtkScrolledWindow *scroll_window;
        GtkBox            *main_box;

        /*条目散列表*/
        ObjectHash *item_table;
};

G_DEFINE_FINAL_TYPE(EventWindow, event_window, ADW_TYPE_APPLICATION_WINDOW)

static void
event_window_finalize(GObject *object)
{
        EventWindow *window = EVENT_WINDOW(object);
        G_OBJECT_CLASS(event_window_parent_class)->finalize(object);

        object_unref(window->item_table);

        OBJECT_INFO_TOOL(EventWindow, finalize, "已清理\n");
}

static void
event_window_class_init(EventWindowClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS(klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);

        gtk_widget_class_set_template_from_resource(
                widget_class,
                "/org/korack/event/EventWindow.ui");
        gtk_widget_class_bind_template_child(widget_class,
                                             EventWindow,
                                             add_button);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventWindow,
                                             header_bar);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventWindow,
                                             scroll_window);
        gtk_widget_class_bind_template_child(widget_class,
                                             EventWindow,
                                             main_box);

        object_class->finalize = event_window_finalize;
}

static void
clean_all_item_from_main_box(EventWindow *self)
{
        GtkWidget    *widget;
        ObjectHashKV *kv;

        object_hash_iter_reset(self->item_table);

        while ((kv = object_hash_iter_get(self->item_table))) {
                widget = object_hash_kv_get_value(kv);
                gtk_box_remove(self->main_box, widget);
        }

        if (self->item_table->main_table)
                free(self->item_table->main_table);
        object_hash_init(self->item_table);
        object_hash_set_improve_size(self->item_table, 30);
}

void
event_window_takle_with_response(EventWindow *self, ObjectString *response)
{
        ObjectStringApplication *str_app =
                object_string_application_new_with(response->charset);

        clean_all_item_from_main_box(self);

        object_string_application_set_segment_charset(str_app, '\n');
        object_string_application_rewind(str_app);
        ObjectStringApplication *line, *tmp, *tokens[4];
        gint                     index = 1;

        line = object_string_application_get_string_before_segment(str_app);
        if (!line) {
                OBJECT_ERROR_TOOL(EventWindow,
                                  takle_with_response,
                                  "服务端传回了不可理解的信息！[%s]\n",
                                  response->charset);
                object_unref(str_app);
                return;
        }

        object_string_application_set_segment_charset(line, '&');
        object_string_application_rewind(line);
        tmp = object_string_application_get_string_before_segment(line);

        if (object_string_compare_with_charset(OBJECT_STRING(tmp),
                                               "Response") == false) {
                OBJECT_WARNING_TOOL(EventWindow,
                                    takle_with_response,
                                    "服务器返回的信息: [%s]\n",
                                    response->charset);
                object_unref(tmp);
                object_unref(line);
                object_unref(str_app);
                return;
        }

        object_unref(tmp);
        object_unref(line);
retry:
        line = object_string_application_get_string_before_segment(str_app);
        if (!line) {
                object_unref(str_app);
                return;
        }

        object_string_application_set_segment_charset(line, '&');
        object_string_application_rewind(line);

        /*获取到日期信息*/
        tokens[0] = object_string_application_get_string_before_segment(line);
        tokens[1] = object_string_application_get_string_before_segment(line);
        tokens[2] = object_string_application_get_string_before_segment(line);
        tokens[3] = object_string_application_get_string_before_segment(line);

        if (!tokens[3]) {
                OBJECT_ERROR_TOOL(
                        EventWindow,
                        takle_with_response,
                        "不正确的解析: %s\n",
                        object_string_get_string(OBJECT_STRING(line)));
                for (int i = 0; i < 4 && tokens[i]; ++i)
                        object_unref(tokens[i]);
                object_unref(line);
                goto retry;
        }

        EventItem *item = event_item_new();
        event_item_set_info(item,
                            index++,
                            object_string_get_string(OBJECT_STRING(tokens[1])),
                            object_string_get_string(OBJECT_STRING(tokens[2])),
                            object_string_get_string(OBJECT_STRING(tokens[0])));
        gtk_box_append(self->main_box, GTK_WIDGET(item));
        object_hash_set_value(self->item_table, (char *)item, true, item, NULL);

        for (int i = 0; i < 4; ++i)
                object_unref(tokens[i]);
        object_unref(line);
        goto retry;
}

static void
event_window_init(EventWindow *self)
{
        gtk_widget_init_template(GTK_WIDGET(self));

        self->item_table = object_hash_new();
        object_hash_set_improve_size(self->item_table, 30);
}
